package com.tungse.study.nio;

import org.junit.Test;

import java.nio.ByteBuffer;
import java.nio.channels.NonWritableChannelException;

/**
 * @author Tungse
 * buffer 缓冲区的操作
 *
 * Buffer常见方法
 * Buffer clear() 清空缓冲区并返回对缓冲区的引用
 * Buffer flip() 为 将缓冲区的界限设置为当前位置，
 * 并将当前位置充值为 0
 * int capacity() 返回 Buffer 的 capacity 大小
 * boolean hasRemaining() 判断缓冲区中是否还有元素
 * int limit() 返回 Buffer 的界限(limit) 的位置
 * Buffer limit(int n) 将设置缓冲区界限为 n,
 *  并返回一个具有新 limit 的缓冲区对象
 * Buffer mark() 对缓冲区设置标记
 * int position() 返回缓冲区的当前位置 position
 * Buffer position(int n) 将设置缓冲区的当前位置为 n，
 * 并返回修改后的 Buffer 对象
 * int remaining() 返回 position 和 limit 之间的元素个数
 * Buffer reset() 将位置 position 转到以前设置的mark
 * 所在的位置
 * Buffer rewind() 将位置设为为 0， 取消设置的 mark
 */
public class Buffer_Test {

    @Test
    public void test02(){
        ByteBuffer buffer = ByteBuffer.allocate(10);
        System.out.println(buffer.position());  //0
        System.out.println(buffer.limit());     //10
        System.out.println(buffer.capacity());  //10
        System.out.println("------------------------");

        //2 put 添加数据到缓冲区
        String name = "tungse";
        buffer.put(name.getBytes());
        System.out.println(buffer.position());  //6
        System.out.println(buffer.limit());     //10
        System.out.println(buffer.capacity());  //10
        System.out.println("------------------------");

        //3 清除缓冲区的数据
        buffer.clear();
        System.out.println(buffer.position());  //0
        System.out.println(buffer.limit());     //10
        System.out.println(buffer.capacity());  //10
        System.out.println("------------------------");

        //4 定义一个新的缓冲区
        ByteBuffer buffer2 = ByteBuffer.allocate(10);
        buffer2.put("tungse".getBytes());
        buffer2.flip();

        //5 get 读取数据
        byte[] bs = new byte[2];
        buffer2.get(bs);
        String result = new String(bs);
        System.out.println(result);

        System.out.println(buffer2.position());  //2
        System.out.println(buffer2.limit());     //6
        System.out.println(buffer2.capacity());  //10

        System.out.println("------------------------");
        buffer2.mark();  //标记
        byte[] bs2 = new byte[3];
        buffer2.get(bs2);
        String result2 = new String(bs2);
        System.out.println(result2);
        System.out.println(buffer2.position());  //5
        System.out.println(buffer2.limit());     //6
        System.out.println(buffer2.capacity());  //10

        System.out.println("------------------------");
        buffer2.reset();  //重置到上一次的标记位置 buffer2.mark();
        if (buffer2.hasRemaining()){
            byte[] bs3 = new byte[buffer2.remaining()];
            System.out.println(buffer2.remaining());
        }
    }



    @Test
    public void test01(){
        ByteBuffer buffer = ByteBuffer.allocate(10);
        System.out.println(buffer.position());  //0
        System.out.println(buffer.limit());     //10
        System.out.println(buffer.capacity());  //10
        System.out.println("------------------------");

        //2 put 添加数据到缓冲区
        String name = "tungse";
        buffer.put(name.getBytes());
        System.out.println(buffer.position());  //6
        System.out.println(buffer.limit());     //10
        System.out.println(buffer.capacity());  //10
        System.out.println("------------------------");

        //3 flip 切换到读取数据模式
        buffer.flip();
        System.out.println(buffer.position());  //0
        System.out.println(buffer.limit());     //6
        System.out.println(buffer.capacity());  //10
        System.out.println("------------------------");

        //4 get 读取数据
        byte b = buffer.get();
        System.out.println((char)b);
        System.out.println(buffer.position());  //1
        System.out.println(buffer.limit());     //6
        System.out.println(buffer.capacity());  //10
    }
}
